<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/28
 * Time: 下午 01:21
 */

namespace app\admin\controller;


use think\Exception;
use think\Request;

class Gift extends Common
{
    /**
     * @return \think\response\View
     * 活动列表
     */
    public function index()
    {
        $gift_name = '';
        $data = [];
        if(Request::instance()->isPost()){
            $gift_name = input("post.gift_name");
            $data['gift_name'] = $gift_name;
        }
        $gift = new \app\admin\model\Gift();
        $pageNum = 1;
        if (is_numeric(input('post.pageNum'))) {
            $pageNum = input('post.pageNum');
        }
        $data['pageNum'] = $pageNum;
        $num_per_page = config('num_per_page');
        $data['num_per_page'] = $num_per_page;
        $list = $gift->getList($gift_name,$pageNum,$num_per_page);
        $count = $gift->getCount($gift_name);
        $data['list'] = $list;
        $data['count'] = $count;
        return view('',$data);
    }

    /**
     * @return \think\response\Json|\think\response\View
     * @throws \Exception
     * 添加
     */
    public function add()
    {
        if(Request::instance()->isPost()){
            $info = input("post.");
            if ($info['gift_name'] == '') {
                return json([
                    'statusCode' => 300,
                    'message' => '礼物名称不能为空',
                ]);
            }
            $gift_name = db("gift")->field('id')->where('gift_name',$info['gift_name'])->find();
            if ($gift_name) {
                return json([
                    'statusCode' => 300,
                    'message' => '礼物名称不能重复',
                ]);
            }
            if ($info['gift_img'] == '') {
                return json([
                    'statusCode' => 300,
                    'message' => '请上传图片',
                ]);
            }
            if ($info['sort'] == '') {
                return json([
                    'statusCode' => 300,
                    'message' => '排序不能为空',
                ]);
            }
            try {
                $gift = new \app\admin\model\Gift();
                $gift->doAdd($info);
            } catch (Exception $e) {
                return json([
                    'statusCode' => 300,
                    'message' => $e->getMessage(),
                ]);
            }
            return json([
                'statusCode' => 200,
                'message' => '添加成功',
                'navTabId' => 'giftManage',
                'callbackType' => 'closeCurrent',
            ]);
        }
        return view();
    }

    /**
     * @return \think\response\Json|\think\response\View
     * 修改
     */
    public function edit()
    {
        $id = input('get.id');
        if(Request::instance()->isPost()){
            $info = input("post.");
            if ($info['gift_name'] == '') {
                return json([
                    'statusCode' => 300,
                    'message' => '礼物名称不能为空',
                ]);
            }
            if ($info['gift_img'] == '') {
                return json([
                    'statusCode' => 300,
                    'message' => '请上传图片',
                ]);
            }
            if ($info['sort'] == '') {
                return json([
                    'statusCode' => 300,
                    'message' => '排序不能为空',
                ]);
            }
            try {
                $gift = new \app\admin\model\Gift();
                $gift->doEdit($info);
            } catch (Exception $e) {
                return json([
                    'statusCode' => 300,
                    'message' => $e->getMessage(),
                ]);
            }
            return json([
                'statusCode' => 200,
                'message' => '修改成功',
                'navTabId' => 'giftManage',
                'callbackType' => 'closeCurrent',
            ]);
        }
        $gift = db("gift")
            ->where("id",$id)
            ->where("is_delete",0)
            ->find();
        $data['gift'] = $gift;
        return view("modify",$data);
    }

    public function del()
    {
        $id = input('get.id');
        try {
            $new_del = new \app\admin\model\Gift();
            $new_del->doDelete($id);
        } catch (Exception $e) {
            return json([
                'statusCode' => 300,
                'message' => $e->getMessage(),
            ]);
        }
        return json([
            'statusCode' => 200,
            'message' => '删除成功',
            'navTabId' => 'giftManage',
        ]);
    }
}